# -*- coding: utf-8 -*-

from pages.basePage import BaseHandler
import jinja2
import os
from models.usuario import Usuario
from models.persona import Persona

jinja_environment = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
        
class PersonaCoisada:
    acaoReflexao = None
    monoPolicromatico = None
    coletivoIndividual = None
    similaridade = None
    pid = None

class Similares(BaseHandler):
    def get(self):
        userId = self.session.get('userid')        
        user = None
        if userId is not None:
            user = Usuario.get_by_id(userId)
        if user is None:
            self.redirect('/')
            return
        url = self.session.get('url')
        if url is None:
            self.redirect('/')
            return

        cenarioSessao = self.session.get('cenarioSessao')
        if cenarioSessao is None:
            self.redirect('/cenario')
            return
            
        personaId = self.request.get('pid')
        if personaId is None:
                self.redirect('/cenario?vinc=1')
                return
        
        persona = Persona.get_by_id(long(personaId))
        
        excluded_pids = cenarioSessao['similares']        
        
        similares = persona.getSimilares(excluded_pids)
        
        
        personasCoisadas = []
        tamanho = len(similares)
        if tamanho > 0:
            if tamanho > 8:
                tamanho = 8
            for i in range (0,tamanho):
                personaCoisada = PersonaCoisada()
                personaCoisada.acaoReflexao = similares[i]['persona'].acaoReflexao
                personaCoisada.monoPolicromatico = similares[i]['persona'].monoPolicromatico
                personaCoisada.coletivoIndividual = similares[i]['persona'].coletivoIndividual
                personaCoisada.similaridade = similares[i]['semelhanca']
                personaCoisada.pid = similares[i]['persona'].key().id()
                personasCoisadas.append(personaCoisada)
        
        template_values = {
            'user' : user,
            'pid' : personaId,
            'similares' : personasCoisadas,
            'url' : url
        }
            
        template = jinja_environment.get_template('html/similares.html')
        self.response.out.write(template.render(template_values))
    def post(self):
        userId = self.session.get('userid')        
        user = None
        if userId is not None:
            user = Usuario.get_by_id(userId)
        if user is None:
            self.redirect('/')
            return
        
        cenarioSessao = self.session.get('cenarioSessao')
        if cenarioSessao is None:
            self.redirect('/cenario')
            return
        
        similares_pids = self.request.get_all('opcao')
           
        if similares_pids: 
            for pid in similares_pids:
                cenarioSessao['similares'].append(long(pid))
            
        self.session['cenarioSessao'] = cenarioSessao
            
        self.redirect('/cenario?vinc=1')